package com.lhwl.it.util;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.jdbc.OracleDialect;
import org.apache.ibatis.session.PageResultList;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;



/**
 * @author Likongyou 2011-04-02
 * 
 *
 */

@Repository
public class BaseDao   {
	@Autowired
	private SqlSession sqlSession;
	
	{
		DefaultSqlSession.setDialect(OracleDialect.class);
	}
	
	
	public PageBean getPage(String sql,HashMap map) {
		int page=DataUtil.getInt(Pconstants.PAGECURPAGE, map,1);
		int pagesize=DataUtil.getInt(Pconstants.PAGESIZE, map,Pconstants.DEF_PAGESIZE);		
		PageBean pageb=new PageBean(page,pagesize);		
		
		PageResultList list=(PageResultList)sqlSession.selectList(sql, map, new RowBounds(pageb.getOffset(),pageb.getPagesize(),true));		
		pageb.setList(list.getData());
		pageb.setRowscount(list.getRowTotal());
		
		//List list=sqlSession.selectList(sql, map);
		//pageb.setList(list);
		//pageb.setRowscount(list.size());
		
		pageb.inst();
		
		return pageb;
	}


	
	public List list(String sql,HashMap map) {		
		return sqlSession.selectList(sql,map);
	}
	
	public HashMap get(String sql,String id) {		
		return (HashMap)sqlSession.selectOne(sql, id);
	}	
	
	public int insert(String sql,HashMap map) {
		return  sqlSession.insert(sql,map);
	}
	
	
	public int update(String  sql,HashMap map) {
		return  sqlSession.update(sql,map);
	}
	
	public int delete(String sql,HashMap map) {
		return sqlSession.delete(sql, map);
	}
	
	public HashMap getMap(String sql,HashMap map) {		
		return (HashMap)sqlSession.selectOne(sql, map);
	}	
	
}
